// pages/zs/index.js
const {
    http
  } = require("../../../../utils/apiutils")
Page({

    /**
     * 页面的初始数据
     */
    data: {
        selindex:0,
        dataList: [],
        jobData: {},
        id: '',
        imgDraw: {},
        sharePath: '',
    qrcode: '',
    shareImgUrl:''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            id: options.id,
            factoryJobId: options.factoryJobId,
            shareName: getApp().globalData.userInfo.realName,
            sharePhone:getApp().globalData.userInfo.phone
        })
        
        this.getJobData()
        this.getCode()
        this.getData()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    onChange(e){
        this.setData({
            selindex:e.detail.current
        })
    },
    showimg(e){
        let url=e.currentTarget.dataset.url;
        wx.previewImage({
          urls: [url],
        })
    },
    //获取工作详情信息
  getJobData: function () {
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })
    http.getJobData(this.data.id,
      function (res) {
        wx.hideLoading()
        console.log("------岗位详情---", res);
        if (res.code == 0) {
          res.data.image = res.data.image.split(',') || [];
          // res.data.type=2;
          that.setData({
            jobData: res.data,
          })
          
        } else {
          wx.showToast({
            title: res.message,
            icon: "none",
          })
        }
      },
      function (err) {
        wx.hideLoading()
      })
    },
    getData() {
        // 获取海报列表
        http.placardList({id:this.data.id},res=>{
            if(res.status==200){
                this.setData({
                    dataList:res.obj.records[0]?res.obj.records[0].placardUrl.split(','):[]
                })
                console.log(this.data.dataList)
            }else{
                wx.showToast({
                  title: res.message,
                })
            }
        },err=>{})
    },
    saveimg(e){
       console.log('执行saveimg')
        let _this  =this;
        wx.showLoading({
            mask:true
        })
        wx.saveImageToPhotosAlbum({
          filePath:e.detail.path,
          success(res1) {
              wx.hideLoading()
              wx.showToast({
                title: '保存成功',
                duration: 3000,
                icon:"none"
              })
          },
          fail(err) {
            wx.hideLoading()
              console.log(err,'错误提示')
              wx.showToast({
                  title: '保存失败',
                  icon:"none"
                })
          }
        })
    },
  getImgInfo() { 
    let _this = this;
        return new Promise((rev, rej) => { 
            wx.getImageInfo({
                src: _this.data.dataList[_this.data.selindex].replace('http','https'),
                success: (res) => {
                    rev(res)
                },
                fail: (err) => {
                  rej(err);
                }
              });
        })
    },
    async draw() {
      wx.showLoading({
        mask: true,
        title:'下载中'
      })
      let imgData = await this.getImgInfo();
      console.log(imgData)
      let zoom = imgData.width / 750;
        let _this = this;
            this.setData({
                imgDraw: {
                    width: imgData.width/zoom+'rpx',
                    height:imgData.height/zoom+'rpx',
                  css:{
                    position:'relative',
                  },
                    views: [
                        {
                            type: 'image',
                            url: this.data.dataList[this.data.selindex].replace('http','https'),
                            // url: 'http://linghuoimage.xbbzp.com/Fk82xa-Dq_FWuZ4d6tr79WAw02wC',
                            mode:'widthFix',
                            css: {
                                width: imgData.width/zoom+'rpx',
                                height:imgData.height/zoom+'rpx',
                                display:'inherit'
                            }
                    },
                        {
                            type: 'image',
                            url: 'http://linghuoimage.xbbzp.com/FsqR44TJHZEjiPSYK7sCwYmlE-EF'.replace('http','https'),
                            mode:'widthFix',
                            css: {
                                position: 'absolute',
                                bottom: '0',
                                width: '100%',
                                display:'inherit'
                            }
                        },
                        {
                            type: 'rect',
                            css: {
                                position:'absolute',
                                borderRadius: '20rpx',
                                width: 690+'rpx',
                                height: '200rpx',
                                color:'#fff',
                                left: 30+ 'rpx',
                                bottom: '30rpx',
                            }
                        },
                        {
                            type: 'text',
                            text: '联系人:'+getApp().globalData.userInfo.realName||'',
                            css: {
                                left: '60rpx',
                                bottom:'170rpx',
                                fontSize: '16px',
                                position:'absolute'
                            }
                        },
                        {
                            type: 'text',
                            text: '电话:'+getApp().globalData.userInfo.phone||'',
                            css: {
                                left: '60rpx',
                                bottom:'130rpx',
                                fontSize: '16px',
                                position:'absolute'
                            }
                        },
                        {
                            type: 'rect',
                            css: {
                                position:'absolute',
                                borderRadius: '20rpx',
                                width: '220rpx',
                                height:'40rpx',
                                color:'#FE6103',
                                left: 60+ 'rpx',
                                bottom: '70rpx',
                            }
                        },
                        {
                            type: 'text',
                            text: '长按识别二维码',
                            css: {
                                left: '80rpx',
                                bottom:'82rpx',
                                fontSize: '12px',
                                position: 'absolute',
                                color:'#fff',
                            }
                        },
                        {
                            type: 'image',
                            url: _this.data.qrcode,
                            mode:'widthFix',
                            css: {
                                position: 'absolute',
                                bottom: '50rpx',
                                width: '150rpx',
                                right:'60rpx',
                                display:'inherit'
                            }
                        },
                  ]
                }
            })
    },
    onImgOK(e) {
      try {
        this.saveimg(e)
       } catch (err) {
        console.log('saveimg错误信息',err)
       }
        
        // this.setData({
        //   sharePath: e.detail.path,
        //   visible: true,
        // })
        // //通知外部绘制完成，重置isCanDraw为false
        // this.triggerEvent('initData') 
    },
    getCode(){
		let params={
			path:'pages/index/index?route=work&id='+this.data.id+'&code='+getApp().globalData.userInfo.inviteCode,
		}
		http.createQrcode4Job(params,res=>{
            this.setData({
                qrcode:'data:image/png;base64,'+res
            })
		},err=>{})
  },
})